<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Cache; // 增加类 by Fire

/**
 * App话题管理
 *
 * @icon fa fa-circle-o
 */
class Topic extends Api
{
    // 设置无需会员登陆
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    protected $redis = null;

    public function _initialize()
    {
        parent::_initialize();
        // 实例化缓存 by Fire
        $options = config('cache');
        $options['prefix'] = 'gfw_topic';
        $options['select'] = 5;
        $this->redis = Cache::connect($options);
    }

    /**
     * 话题热点固定话题
     */
    public function hotTopic()
    {
        $list = model('app.topic')->where('name', 'in', '#楼市爆料#,#真实楼价#,#问一问#')->field('id,name,countpv,addpv')->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $row[$k]['id'] = $v['id'];
                $row[$k]['name'] = $v['name'];

                $pv = $v['countpv'] + $v['addpv'];
                if( $pv >= 10000){
                    $pvThousand = $pv / 10000;
                    $pv = sprintf("%.2f", $pvThousand) . '万';
                }
                $row[$k]['pv'] = $pv;
            }

            $this->success('操作成功', $row);
        } else {
            $this->error();
        }
    }

    /**
     * 48小时热门话题
     */
    public function topic48Hours()
    {
        // 暂时为热点的话题
        $list = model('app.topic')->where('hotswitch',1)->field('id,name')->limit(6)->order('updatetime desc')->select();
        if ($list){
            $this->success('操作成功', $list);
        } else {
            $this->error();
        }
    }

    /**
     * 话题搜索
     */
    public function searchTopicList()
    {
        $page    = input('page', 1);
        $limit   = 10;
        $topicName = input('name', '');
        $where = [];
        if ($topicName) {
            $topicNameRes = trim($topicName, '#');
            $where['name'] = ['like', '%' . $topicNameRes . '%'];
        }

        $topic = model('app.topic');
        $total = $topic->where($where)->count();
        $list = $topic->where($where)->field('id,name,countpv,countshare,countpraise,addpv,addshare,addpraise')->order('countpv desc')->paginate($limit, $total, ['page' => $page]);

        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['pv'] = $v['countpv'] + $v['addpv'];
                $list[$k]['share'] = $v['countshare'] + $v['addshare'];
                $list[$k]['praise'] = $v['countpraise'] + $v['addpraise'];
                unset($list[$k]['countpv'],$list[$k]['countshare'],$list[$k]['countpraise'],$list[$k]['addpv'],$list[$k]['addshare'],$list[$k]['addpraise']);
            }

            $this->success('操作成功', $list);
        } else {
            $this->error();
        }
    }

    /**
     * 计划任务 - 2小时统计
     */
    public function count2Hours()
    {
        $topic = model('app.topic')->field('id,countisr,countpv,countshare,countpraise,countcomment')->order('id', 'desc')->select();
        if ($topic) {
            foreach ($topic as $k => $v) {
                $countIntel = $this->redis->get($v['id'].'_countIntel');
                $countPv = $this->redis->get($v['id'].'_countPv');
                $countShare = $this->redis->get($v['id'].'_countShare');
                $countPraise = $this->redis->get($v['id'].'_countPraise');
                $countComment = $this->redis->get($v['id'].'_countComment');

                if ($countIntel) {
                    $v->countisr += $countIntel;
                    $this->redis->set($v['id'].'_countIntel', 0);
                }
                if ($countPv) {
                    $v->countpv += $countPv;
                    $this->redis->set($v['id'].'_countPv', 0);
                }
                if ($countShare) {
                    $v->countshare += $countShare;
                    $this->redis->set($v['id'].'_countShare', 0);
                }
                if ($countPraise) {
                    $v->countpraise += $countPraise;
                    $this->redis->set($v['id'].'_countPraise', 0);
                }
                if ($countComment) {
                    $v->countcomment += $countComment;
                    $this->redis->set($v['id'].'_countComment', 0);
                }

                if ($countIntel || $countPv || $countShare || $countPraise || $countComment) {
                    $v->save();
                }
            }
            $this->success('2小时统计完成');
        }
    }

    /**
     * 计划任务 - 48小时统计
     */
    public function count48Hours()
    {
        $topic = model('app.topic')->field('id,countisr,countpv,countshare,countpraise,countcomment,twodayspv,twodaysshare,twodayspraise,twodayscomment')->select();
        if ($topic) {
            foreach ($topic as $k => $v) {
                $countPv = $this->redis->get($v['id'].'_countPvTotal');
                $countShare = $this->redis->get($v['id'].'_countShareTotal');
                $countPraise = $this->redis->get($v['id'].'_countPraiseTotal');
                $countComment = $this->redis->get($v['id'].'_countCommentTotal');

                if ($countPv) {
                    $v->twodayspv = $v->countpv - $countPv;
                }

                if ($countShare) {
                    $v->twodaysshare = $v->countshare - $countShare;
                }

                if ($countPraise) {
                    $v->twodayspraise = $v->countpraise - $countPraise;
                }

                if ($countComment) {
                    $v->twodayscomment = $v->countcomment - $countComment;
                }

                if ($countPv || $countShare || $countPraise || $countComment) {
                    $v->save();
                }

                $this->redis->set($v['id'].'_countPvTotal', $v->countpv);
                $this->redis->set($v['id'].'_countShareTotal', $v->countshare);
                $this->redis->set($v['id'].'_countPraiseTotal', $v->countpraise);
                $this->redis->set($v['id'].'_countCommentTotal', $v->countcomment);
            }
            $this->success('48小时统计完成');
        }
    }

    /**
     * 计划任务 - 数据清理
     */
    public function clearZero()
    {
        $topic = model('app.topic')->field('id, countisr, createtime')->select();
        if ($topic) {
            foreach ($topic as $k => $v) {
                if ($v['countisr'] <= 0 && $v['createtime'] < strtotime("-15 day")) {
                    $v->delete();
                    $this->redis->rm($v['id'].'_countIntel');
                    $this->redis->rm($v['id'].'_countPv');
                    $this->redis->rm($v['id'].'_countShare');
                    $this->redis->rm($v['id'].'_countPraise');
                    $this->redis->rm($v['id'].'_countComment');
                    $this->redis->rm($v['id'].'_countPvTotal');
                    $this->redis->rm($v['id'].'_countShareTotal');
                    $this->redis->rm($v['id'].'_countPraiseTotal');
                    $this->redis->rm($v['id'].'_countCommentTotal');
                }
            }
            $this->success('数据清理完成');
        }
    }
}
